<?php
/**
 * Geneone: Content Management Framework
 * Discussion Comment Object View
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
require_once ("Gene/Object/View.php");

class Gene_Object_View_Discussioncomment extends Gene_Object_View {
	/**
	 * Class Constructor
	 *
	 * @var object $model Gene_Object_Model object
	 */
	function Gene_Object_View_Discussioncomment(&$model) {
		$this->model =& $model;
	}
	
	/**
	 * Show this object. Returns HTML output code for object.
	 *
	 * @var array $actions Additional Arguments
	 *
	 * @return string HTML output for object.
	 */
	function view($actions=array()) {
		$db =& Gene::getDatabase();
		$user =& Gene::getUser();
		
		if (isset($actions[2]) && $actions[2] == "report" && $user->getUserId()) {
			$tpl =& Gene::getTemplate();
			
			/* Parents */
			$parents = $this->model->getParents();
			$tpl->assign ("parents", $parents);
			
			$report =& $this->model->getReport();
			
			$info = $this->model->get();
			if ($reportinfo = $report->get()) {
				$tpl->assign ("reported", true);
				$tpl->assign ("report", $reportinfo);
			}
			
			if (!$info['owner_id']) {
				$info = array_merge ($this->model->getAdditionalInfo(Gene::tableName("object_guests")), $info);
			}
			$tpl->assign ("object", $info);
			
			if ($_SERVER['REQUEST_METHOD'] == "POST") {
				$remove = Gene_Request::getBoolean("remove", GENE_REQUEST_POST);
				if ($remove) {
					$report->remove();
					header ("Location: ".Gene::makeLink($info));
					exit();
				} else {
					$type = Gene_Request::getInteger("complaint", GENE_REQUEST_POST);
					$comments = Gene_Request::getString("additional", GENE_REQUEST_POST);

					if ($report->set($type, $comments)) {
						header ("Location: ".Gene::makeLink($info));
						exit();
					} else {
						$tpl->assign ("errormsg", "There was an error reporting this comment.");
					}
				}
			}
			
			$tpl->append ("stylesheets", "discussion");
			
			return $tpl->fetch("objects/discussioncomment/view_report");
		} else {
			$info = $this->model->get();
			$parent =& Gene_Object::get($info['parent']);
			$parentinfo = $parent->get();
			
			$q = $db->prepare ("SELECT COUNT(object_id) as postsbefore FROM ".Gene::tableName("objects")." WHERE parent=? AND t_create < ?");
			$res = $q->execute(array($parentinfo['object_id'], $info['t_create']));
			$postsbefore = $res->fetchOne();
			$page = floor($postsbefore/25)+1;
	
			$url = Gene::makeLink($parent);
			$parsed = parse_url($url);
			if ($parsed['query']) {
				$url = $url."&page=".$page;
			} else {
				$url = $url."?page=".$page;
			}
	
			header ("Location: ".$url."#comment".$info['object_id']);
			exit();
		}
	}
	
	/**
	 * Displays the object creation form for this type of object.
	 *
	 * @static
	 *
	 * @var object $parent Parent Object
	 *
	 * @return string HTML output for object creation.
	 */
	function create(&$parent) {
		$tpl =& Gene::getTemplate();
		
		$info = $parent->get();
		
		/* Taxonomy & Similar Entries */
		$taxonomy =& $parent->getTaxonomyEntry($info['parent']);
		$tpl->assign ("tags", $taxonomy->getAll());
		
		// User Forum Tags
		$p = Gene_Object::get($info['parent']);
		$userforumtag =& $p->getUserTaxonomySet();
		$tpl->assign ("userforumtags", $userforumtag->getLabels());
		
		// Get User Tags for this discussion
		$usertag =& $parent->getUserTaxonomyEntry($info['parent']);
		$tpl->assign ("usertags", $usertag->getAll());
		
		// Quote?
		$quote = Gene_Request::getInteger("quote", GENE_REQUEST_GET);
		if ($quote) {
			$q =& Gene_Object::get($quote);
			if ($q) {
				$qinfo = $q->get();
			
				if ($qinfo['type'] == 'discussion' && $qinfo['object_id'] == $info['object_id'] && $q->canView()) {
					$tpl->assign ("quote", $qinfo);
				} else if ($qinfo['type'] == 'discussioncomment' && $qinfo['parent'] == $info['object_id'] && $q->canView()) {
					$tpl->assign ("quote", $qinfo);
				}
			}
		}
			
		return $tpl->fetch("objects/discussioncomment/create");
	}
	
	/**
	 * Creates the new object.
	 * 
	 * Objects can implement their own creation routines here to fiddle
	 * the user input before sending to Gene_Object::create() or return a
	 * PEAR::raiseError if input is invalid
	 */
	function doCreate($type, $title='', $desc='', $content='', &$parentobj, $options=array(), $draft=false) {
		$options['tags'] = explode(" ", Gene_Request::getString("tags", GENE_REQUEST_POST));
		
		return parent::doCreate($type, $title, $desc, $content, $parentobj, $options, $draft);
	}
}
 
?>